//
//  PKNavigationView.m
//  PDFKitTest
//
//  Created by Emo, on 2024/1/23.
//

#import "PKNavigationView.h"

@interface PKNavigationView ()
/** 编辑或预览按钮*/
@property (nonatomic, strong) UIButton * editOrPreviewBtn;
/** 保存修改按钮*/
@property (nonatomic, strong) UIButton * saveBtn;
/** 添加功能按钮*/
@property (nonatomic, strong) UIButton * moreBtn;
/** 清理按钮*/
@property (nonatomic, strong) UIButton * cleanBtn;

@end

@implementation PKNavigationView

- (instancetype)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, DON_ScreenW, DON_NavBarH);
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.editOrPreviewBtn];
    [self addSubview:self.saveBtn];
    [self addSubview:self.cleanBtn];
    [self addSubview:self.moreBtn];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.editOrPreviewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(8);
        make.bottom.mas_equalTo(self);
        make.width.height.mas_equalTo(DON_NavH);
    }];
    
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.editOrPreviewBtn);
        make.left.mas_equalTo(self.editOrPreviewBtn.mas_right);
        make.width.height.mas_equalTo(DON_NavH);
    }];
    
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.editOrPreviewBtn);
        make.right.mas_equalTo(self).offset(-8);
        make.width.height.mas_equalTo(DON_NavH);
    }];
    
    [self.cleanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.editOrPreviewBtn);
        make.right.mas_equalTo(self.moreBtn.mas_left);
        make.width.height.mas_equalTo(DON_NavH);
    }];
}


#pragma mark -- click --
- (void)editOrPreviewBtnClick:(UIButton *)sender {
    self.cleanBtn.hidden = self.moreBtn.hidden = sender.selected;
    self.saveBtn.hidden = NO;
    sender.selected = !sender.selected;
    if (sender.selected) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(navigationViewDidClickEdit)]) {
            [self.delegate navigationViewDidClickEdit];
        }
    } else {
        if (self.delegate && [self.delegate respondsToSelector:@selector(navigationViewDidClickPreview)]) {
            [self.delegate navigationViewDidClickPreview];
        }
    }
}

- (void)saveBtnClick:(UIButton *)sender {
    self.editOrPreviewBtn.selected = NO;
    self.saveBtn.hidden = self.cleanBtn.hidden = self.moreBtn.hidden = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(navigationViewDidClickSave)]) {
        [self.delegate navigationViewDidClickSave];
    }
}

- (void)moreBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(navigationViewDidClickMore:)]) {
        [self.delegate navigationViewDidClickMore:sender];
    }
}

- (void)cleanBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(navigationViewDidClickClean)]) {
        [self.delegate navigationViewDidClickClean];
    }
}


#pragma mark -- lazy --
- (UIButton *)editOrPreviewBtn {
    if (!_editOrPreviewBtn) {
        _editOrPreviewBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_editOrPreviewBtn setImage:[UIImage imageNamed:@"navi_edit"] forState:(UIControlStateNormal)];
        [_editOrPreviewBtn setImage:[UIImage imageNamed:@"navi_preview"] forState:(UIControlStateSelected)];
        [_editOrPreviewBtn addTarget:self action:@selector(editOrPreviewBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        _editOrPreviewBtn.selected = NO;
    }
    return _editOrPreviewBtn;
}

- (UIButton *)saveBtn {
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_saveBtn setImage:[UIImage imageNamed:@"navi_save"] forState:(UIControlStateNormal)];
        [_saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        _saveBtn.hidden = YES;
    }
    return _saveBtn;
}

- (UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_moreBtn setImage:[UIImage imageNamed:@"navi_add"] forState:(UIControlStateNormal)];
        [_moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        _moreBtn.hidden = YES;
    }
    return _moreBtn;
}

- (UIButton *)cleanBtn {
    if (!_cleanBtn) {
        _cleanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_cleanBtn setImage:[UIImage imageNamed:@"navi_clean"] forState:(UIControlStateNormal)];
        [_cleanBtn addTarget:self action:@selector(cleanBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        _cleanBtn.hidden = YES;
    }
    return _cleanBtn;
}

@end
